﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MINERP.BLL.GoodsDelivery
{
    public class DeliverBackBLL
    {
        //参数
        private DAL.GoodsDelivery.DeliverBackDAL dal = new DAL.GoodsDelivery.DeliverBackDAL();

        #region 按发货单Id和状态查询账单
        /// <summary>
        /// 按发货单Id和状态查询账单
        /// </summary>
        /// <param name="deliverId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.DeliveryBack> GetBillsByDeliverIdAndStatus(int deliverId, int status)
        {
            try
            {
                DataTable dt = dal.GetBillsByDeliverId(deliverId, status);
                return ChangeDtToListDeliverBack(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将发货单状态改为已发送
        /// <summary>
        /// 将发货单状态改为已发送
        /// </summary>
        public void ChangeDeliveryToDeliver(int deliverId,int status)
        {
            try
            {
                dal.ChangeDeliveryToDeliver(deliverId, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将DT转化成List
        /// <summary>
        /// 将DT转化成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<MODEL.GoodsDelivery.BillInfo> ChangeDTToListStatus(DataTable dt)
        {
            try
            {
                List<MODEL.GoodsDelivery.BillInfo> billInfos = new List<MODEL.GoodsDelivery.BillInfo>();
                foreach (DataRow row in dt.Rows)
                {
                    MODEL.GoodsDelivery.BillInfo bill = new MODEL.GoodsDelivery.BillInfo();
                    bill.DeliverId = Convert.ToInt32(row["DeliverId"]);
                    bill.BillId = Convert.ToInt32(row["BillId"]);
                    bill.BillOrderCode = row["BillOrderCode"].ToString();
                    bill.BillCreateTime = Convert.ToDateTime(row["BillCreateTime"]);
                    bill.BusTimetable = row["BusTimetable"].ToString();
                    bill.ConsigneeName = row["ConsigneeName"].ToString();
                    bill.ConsigneeTelePhone = row["ConsigneeTelePhone"].ToString();
                    bill.DistributionAddress = row["DistributionAddress"].ToString();
                    bill.DistributionMode = row["DistributionMode"].ToString();
                    bill.FarePayer = row["FarePayer"].ToString();
                    bill.Remark = row["Remark"].ToString();
                    bill.ShopId = Convert.ToInt32(row["ShopId"]);
                    bill.ShopName = row["ShopName"].ToString();
                    billInfos.Add(bill);
                }
                return billInfos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将DT转化成List(发货反馈)
        /// <summary>
        /// 将DT转化成List(发货反馈)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<MODEL.GoodsDelivery.DeliveryBack> ChangeDtToListDeliverBack(DataTable dt)
        {
            try
            {
                List<MODEL.GoodsDelivery.DeliveryBack> billInfos = new List<MODEL.GoodsDelivery.DeliveryBack>();
                foreach (DataRow r in dt.Rows)
                {
                    MODEL.GoodsDelivery.DeliveryBack bill = new MODEL.GoodsDelivery.DeliveryBack();
                    if (r["ArrivalTime"] != DBNull.Value)
                        bill.ArrivalTime = Convert.ToDateTime(r["ArrivalTime"]);
                    bill.BillId = Convert.ToInt32(r["BillId"]);
                    bill.BillOrderCode = Convert.ToInt32(r["BillOrderCode"]);
                    bill.DeliverId = Convert.ToInt32(r["DeliverId"]);
                    if (r["Remark"] != DBNull.Value)
                        bill.DeliverObject = r["DeliverObject"].ToString();
                    if (r["DeliverTime"] != DBNull.Value)
                        bill.DeliverTime = Convert.ToDateTime(r["DeliverTime"]);
                    if (r["TuoYunDanCode"] != DBNull.Value)
                        bill.TuoYunDanCode = r["TuoYunDanCode"].ToString();
                    bill.Num = Convert.ToInt32(r["Num"]);
                    bill.OtherMoney = Convert.ToDouble(r["OtherMoney"]);
                    if(r["Remark"]!=DBNull.Value)
                        bill.Remark = r["Remark"].ToString();
                    if (r["ReceiveStationRemark"] != DBNull.Value)
                        bill.ReceiveStationRemark = r["ReceiveStationRemark"].ToString();
                    bill.SelfMoney = Convert.ToDouble(r["SelfMoney"]);
                    bill.ShopMoney = Convert.ToDouble(r["ShopMoney"]);
                    bill.ShopName = r["ShopName"].ToString();
                    bill.ShopId = Convert.ToInt32(r["ShopId"]);
                    if (r["Remark"] != DBNull.Value)
                        bill.StationRemark = r["StationRemark"].ToString();
                    if (r["TakeAwayTime"] != DBNull.Value)
                        bill.TakeAwayTime = Convert.ToDateTime(r["TakeAwayTime"]);
                    billInfos.Add(bill);
                }
                return billInfos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改账单的发货信息
        /// <summary>
        /// 修改账单的发货信息
        /// </summary>
        public void SaveBillDeliverInfo(List<int> billIds, DateTime deliverTime, DateTime arrivalTime, string deliverObject, string TuoYunDanCode, int num, string stationRemark, double shopMoney, double selfMoney, double otherMoney, string billRemark, string receiveStationRemark)
        {
            try
            {
                dal.SaveBillDeliverInfo(billIds, deliverTime, arrivalTime, deliverObject, TuoYunDanCode, num, stationRemark, shopMoney, selfMoney, otherMoney, billRemark, receiveStationRemark);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 取消账单
        /// <summary>
        /// 取消账单
        /// </summary>
        /// <param name="billIds"></param>
        public void CancelBills(List<int> billIds)
        {
            try
            {
                dal.CancelBills(billIds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //----------------数据结构----------------
        /// <summary>
        /// 发货单头信息
        /// </summary>
        /// <returns></returns>
        public DataTable CreateDeliverBillInfo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("DeliverId", typeof(int));
            dt.Columns.Add("BillId", typeof(int));
            dt.Columns.Add("BillIndexId", typeof(int));
            dt.Columns.Add("DeliverTime", typeof(DateTime));
            dt.Columns.Add("ArrivalTime", typeof(DateTime));
            dt.Columns.Add("DeliverObject", typeof(string));
            dt.Columns.Add("TuoYunDanCode", typeof(string));
            dt.Columns.Add("Num", typeof(int));
            dt.Columns.Add("StationRemark", typeof(string));
            dt.Columns.Add("ShopMoney", typeof(double));
            dt.Columns.Add("SelfMoney", typeof(double));
            dt.Columns.Add("OtherMoney", typeof(double));
            dt.Columns.Add("ReceiveStationRemark", typeof(string));
            return dt;
        }
    }
}
